package jp.co.withone.osgi.gadget.picasaconnector;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.media.mediarss.MediaContent;
import com.google.gdata.data.media.mediarss.MediaGroup;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.data.photos.GphotoFeed;
import com.google.gdata.data.photos.PhotoFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

/**
 * PicasaConnector.
 * @author ohsoko.hiroshi
 */
public class PicasaConnector {

    private static final String API_PREFIX = "http://picasaweb.google.com/data/feed/api/user/";

    private static final String PARAM_PHOTO = "photo";

    private static final String PARAM_ALBUM = "album";

    private static PicasaConnector connector = null;

    private PicasawebService picasawebService = null;

    boolean isAuthenticated = false;
    private String USER_NAME = null;
    private String PASSWORD = null;

    /**
     * private constructor.
     */
    private PicasaConnector() {
        // TODO app_name
        picasawebService = new PicasawebService("app_name");
    }

    /**
     * singleton instance.
     * @return singleton instance
     */
    public static PicasaConnector getInstance() {
        if (connector == null) {
            connector = new PicasaConnector();
        }
        return connector;
    }

    /**
     * @throws AuthenticationException
     */
    public void authenticate(String userName, String password)
            throws AuthenticationException {
        // can not login

        this.USER_NAME = userName;
        this.PASSWORD = password;

        try {
            this.picasawebService.setUserCredentials(this.USER_NAME,
                    this.PASSWORD);
        } catch (AuthenticationException e) {
            // TODO error
            throw e;
        }
        this.isAuthenticated = true;
    }

    /**
     * 
     * @return
     */
    public List<String> getAlbumNameList() {

        List<String> albumNameList = new ArrayList<String>();
        String albumUrl = API_PREFIX + this.USER_NAME;
        albumUrl = addKindParameter(albumUrl, PARAM_ALBUM);

        AlbumFeed feed = null;
        try {
            feed = getFeed(albumUrl, AlbumFeed.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<GphotoEntry> entries = feed.getEntries();
        for (GphotoEntry gphotoEntry : entries) {
            albumNameList.add(gphotoEntry.getTitle().getPlainText());
        }
        return albumNameList;
    }

    /**
     * 
     * @return
     */
    public List<URL> getPhotosURLList() {
        String albumUrl = API_PREFIX + this.USER_NAME;
        albumUrl = addKindParameter(albumUrl, PARAM_PHOTO);

        PhotoFeed feed = null;
        try {
            feed = getFeed(albumUrl, PhotoFeed.class);
        } catch (Exception e) {
            // TODO error
            return null;
        }
        ArrayList<URL> urlList = new ArrayList<URL>();
        for (GphotoEntry entry : feed.getEntries()) {
            MediaGroup extension = entry.getExtension(MediaGroup.class);
            List<MediaContent> repeatingExtension = extension
                    .getRepeatingExtension(MediaContent.class);
            for (MediaContent content : repeatingExtension) {
                URL url = null;
                try {
                    url = new URL(content.getUrl());
                    urlList.add(url);
                } catch (MalformedURLException e) {
                    // TODO error
                }
            }
        }
        return urlList;
    }

    private <T extends GphotoFeed> T getFeed(String feedURL, Class<T> feedClass)
            throws MalformedURLException, IOException, ServiceException {
        return this.picasawebService.getFeed(new URL(feedURL), feedClass);
    }

    private String addKindParameter(String url, String kind) {
        if (url.contains("?")) {
            return url + "&kind=" + kind;
        } else {
            return url + "?kind=" + kind;
        }
    }
}
